<?php
require_once 'common.php';
require_once 'Student.php';
require_once 'ResultData.php';


if (isGet()) {
    echo "不得以GET访问";
    exit;
}


$name = trim($_REQUEST['name']);
$sex = trim($_REQUEST['sex']);
$age = trim($_REQUEST['age']);
$edu = trim($_REQUEST['edu']);
$city = trim($_REQUEST['city']);
$no = trim($_REQUEST['no']);

$result = new ResultData();
if ($name == "") {
    $result->code = "404";
    $result->msg = "name不能为空";
    exit(json_encode($result));
}

$student = new Student();
$student->name = $name;
$student->sex = $sex;
$student->age = $age;
$student->edu = $edu;
$student->city = $city;
$student->no = $no;

$json_string = file_get_contents('data.json');
// 用参数true把JSON字符串强制转成PHP数组
$data = json_decode($json_string);
$student->id = count($data) + 1;

array_push($data, $student);
$json_string = json_encode(array_values($data));

file_put_contents('data.json', $json_string);

exit(json_encode($result));
